package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Right;
import com.ciei.dpagm.service.RightService;
import com.ciei.dpagm.service.RoleRightService;
import com.ciei.dpagm.service.RoleService;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RightController  extends BaseController {

	@Autowired
	RoleRightService roleRightService;
	@Autowired
	RightService rightService;
	@Autowired
	RoleService roleService;

	@RequestMapping("roleAndPermission/rightPage")
	private String viewTreeList(){
		return "roleAndPermission/rightPage";
	}

	@RequestMapping("roleAndPermission/roleForm")
	public String loginPage() {
		return "roleAndPermission/roleForm";
	}

	@RequestMapping(value = "right/addRight")
	@ResponseBody
	public JSONObject addRight(Right right) {
		return JsonUtil.getFail("不允许新增权限");
	}


	@RequestMapping(value = "right/loadForm")
	@ResponseBody
	public JSONObject getAllRight() {

		List<Right> right = rightService.findAll();
		JSONObject json = new JSONObject();
		JSONArray jarr = new JSONArray();
		if(right!=null && right.size()>0) {
			for (Right right2 : right) {
				json.put("rightName", right2.getRightName());
				jarr.add(json);
			}
		}

		json.put(APP.DATA, jarr);
		return json;
	}

	@RequestMapping(value = "right/loadTree")
	@ResponseBody
	public JSONObject loadTree() {
		JSONObject jsonObject = JsonUtil.getSuccess("");
		jsonObject.put(APP.DATA, rightService.loadTree(false, false));
		return jsonObject;
	}

	@RequestMapping(value = "right/loadRoleRightIds")
	@ResponseBody
	public JSONObject loadRoleRightIds(Integer roleId) {
		JSONObject jsonObject = JsonUtil.getSuccess("");
		jsonObject.put(APP.DATA, roleRightService.loadRightIdsByRoleId(roleId));
		return jsonObject;
	}

	@RequestMapping("right/updateRoleRight")
	@ResponseBody
	public JSONObject updateRoleRight(Integer roleIds,String str) {
		return roleRightService.updateRoleRight(roleIds, str);
	}

	@RequestMapping("right/loadRightGird")
	@ResponseBody
	public JSONObject loadRightGird(@RequestBody JSONObject params) {
		return rightService.loadRightGird(params);
	}

	@RequestMapping("right/disableRight")
	@ResponseBody
	public JSONObject disableRight(Integer rightId, Integer disable) {
		return rightService.disableRight(rightId, disable);
	}

	@RequestMapping("right/delete")
	@ResponseBody
	public JSONObject delete(Integer rightId) {
		return rightService.delete(rightId);
	}

	@RequestMapping("right/save")
	@ResponseBody
	public JSONObject save(@RequestBody JSONObject rightJson) {
		return rightService.saveOrUpdate(rightJson);
	}

	@RequestMapping("right/update")
	@ResponseBody
	public JSONObject update(@RequestBody JSONObject rightJson) {
		return rightService.saveOrUpdate(rightJson);
	}

    @RequestMapping("right/loadRightCodeOptions")
    @ResponseBody
    public JSONObject loadRightCodeOptions() {
        return rightService.loadRightCodeOptions();
    }

	@RequestMapping(value = "right/loadMenuTree")
	@ResponseBody
	public JSONObject loadMenuTree() {
		return JsonUtil.getSuccess("").fluentPut(APP.DATA, rightService.loadTree(false, true));
	}

    // TODO 获取单个权限信息

}
